package com.ycsyxt.guide;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ycs(15241496739 @ 163.com)
 * @date 2022/6/12
 **/
public class Class14 {
    public static void main(String[] args) {
        // 二叉树广度优先遍历
        BinaryNode root = new BinaryNode(1);
        root.left = new BinaryNode(2);
        root.right = new BinaryNode(3);
        root.left.left = new BinaryNode(4);
        root.left.right = new BinaryNode(5);
        root.right.left = new BinaryNode(6);
        root.right.right = new BinaryNode(7);
        System.out.println(bfs(root));
    }

    public static List<Integer> bfs(BinaryNode root) {
        LinkedList<BinaryNode> queue = new LinkedList<>();
        queue.offer(root);
        List<Integer> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            BinaryNode temp = queue.poll();
            result.add(temp.val);
            if (temp.left != null) {
                queue.offer(temp.left);
            }
            if (temp.right != null) {
                queue.offer(temp.right);
            }
        }
        return result;
    }
}